import random
from PIL import Image, ImageDraw, ImageFont,ImageFilter


def generate_random():
    return ''.join(random.sample(
        '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 4))


def generate_captcha_image(random_str):
    image = Image.new('RGB', (100, 38), "#ffffff")
    font = ImageFont.truetype("./arial.ttf", 24)
    draw = ImageDraw.Draw(image)
    draw.text((10, 10), random_str, font=font, fill=tuple([random.randint(64, 180) for _ in range(3)]))
    for i in range(random.randint(2, 4)):
        start = (random.randint(0, 100), random.randint(0, 38))
        end = (random.randint(0, 100), random.randint(0, 38))
        draw.line([start, end], fill=tuple([random.randint(64, 180) for _ in range(3)]))

    image.filter(ImageFilter.EDGE_ENHANCE)
    return image
